<?php

namespace Model\TeacherBundle\Controller;

use Model\TeacherBundle\Entity\Teacher;
use Model\TeacherBundle\Entity\TeacherApply;
use Model\TeacherBundle\Model\TeacherModel;
use Model\TeacherBundle\Setting\TeacherApplySetting;
use Model\UserBundle\Model\UserModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

use Symfony\Component\HttpFoundation\Request;
use Base\BaseController;
use Model\TeacherBundle\Entity\TeacherApply as Entity;

/**
 * @route("/admin/")
 */
class TeacherApplyAdminController extends BaseController
{

    /**
     * @route("TeacherApply/add",name="TeacherApplyPreAdd")
     */
    public function add()
    {
        //通用代码
        $this->setSetting(new TeacherApplySetting($this->getDoctrine()->getManager()));
        return $this->comAdd();
    }

    /**
     * @route("TeacherApply/add/do",name="TeacherApplyDoAdd")
     *
     * @Method({"GET", "POST"})
     */
    public function doAdd(Request $request)
    {
        //私有代码
        $entity = new Entity();
        $entity->setAddTime(time());

        //通用代码
        $this->setSetting(new TeacherApplySetting($this->getDoctrine()->getManager()));
        return $this->comDoAdd($request,$entity);
    }

    /**
     * @route("TeacherApply/edit/{id}",name="TeacherApplyPreEdit")
     */
    public function edit(Entity $entity)
    {
        $this->setSetting(new TeacherApplySetting($this->getDoctrine()->getManager()));
        return $this->comEdit($entity);
    }

    /**
     * @route("TeacherApply/edit/{id}/do",name="TeacherApplyDoEdit")
     *
     * @Method({"GET", "POST"})
     */
    public function doEdit(Request $request,Entity $entity)
    {
        $this->setSetting(new TeacherApplySetting($this->getDoctrine()->getManager()));
        return $this->comDoEdit($request,$entity);
    }

    /**
     * @route("TeacherApply",name="TeacherApplyLists")
     */
    public function lists()
    {
        //私有部分
        $tools = array();

        //通用部分
        $this->setSetting(new TeacherApplySetting($this->getDoctrine()->getManager()));
        return $this->comLists($tools);
    }


    /**
     * @route("TeacherApply/tools",name="TeacherApplyTools")
     */
    public function tools(Request $request)
    {
        $this->setSetting(new TeacherApplySetting($this->getDoctrine()->getManager()));
        return $this->comTools($request);
    }


    /**
     * @route("TeacherApply/view/{id}",name="TeacherApplyView")
     */
    public function TeacherApplyView(Request $request , TeacherApply $TeacherApply)
    {
        $userId = $TeacherApply->getUserId();
        $data["apply"] = $TeacherApply;
        $params = json_decode($TeacherApply->getParams(),true);
        $data["p"] = $params;
        $data["manager"] = isset($params["manager"]) ? $params["manager"] : "";
        $data["org"] = isset($params["organization"]) ? $params["organization"] : "";
        $data["msg"] = isset($params["msg"]) ? $params["msg"] : "";
//        var_dump($TeacherApply);
        return $this->render("ModelTeacherBundle:TeacherApply:view.php.twig",$data);
    }
    /**
     * @route("TeacherApply/deal/{id}",name="TeacherApplyDeal")
     */
    public function TeacherApplyDeal(Request $request , TeacherApply $TeacherApply)
    {
//        var_dump($this->getParamRequired("status") );die;
        $status = $this->getParamRequired("status");
        if (!$status) {return $this->js_out("请选择审核结果");}
        if (!$msg = $this->getParamRequired("msg")) {return $this->js_out("请填写审核意见");}
        $data = array();
        $em = $this->getDoctrine()->getManager();
        if ($status == 1) { //审核通过
            //检查是否创建过教师
            $userId = $TeacherApply->getUserId();
            $TeacherModel = new TeacherModel($this->getDoctrine());
            $hasTeacher = $TeacherModel->checkHasTeacher($userId);
            if (!empty($hasTeacher)) {
                $TeacherName = $hasTeacher["name"];
                return $this->js_out("改用户已创建其他教师[$TeacherName]，不能继续创建");
            }
            //修改审核信息状态
            $TeacherApply->setStatus(1);
            //添加教师,设置用户为教师
            $Teacher = new Teacher();
            $Teacher->setUserId($userId);
            $Teacher->setName($TeacherApply->getName());
            $Teacher->setIntro($TeacherApply->getIntro());
            $Teacher->setParams($TeacherApply->getParams());
            $Teacher->setAddTime(time());
            $Teacher->setSchool(0);
            $Teacher->setCategory($TeacherApply->getCategory());
            $em->persist($Teacher);
            $em->flush();
            //为用户添加教师角色
            $userModel = new UserModel($this->getDoctrine());
            $userModel->updateUserInfo($userId,array("groups"=>7));//todo 此处为硬代码，添加校长角色（考虑优化）

        } else {//审核不通过

            //修改审核信息状态
            $TeacherApply->setStatus(2);

        }
        //将审核信息添加至参数列
        $oldParams = json_decode($TeacherApply->getParams(),true);
        $oldParams["msg"] = $msg;
        $newParams = json_encode($oldParams);
        $TeacherApply->setParams($newParams);
        $em->persist($TeacherApply);
        $em->flush();

        return $this->js_out_close("处理成功");
//        return $this->render("ModelTeacherBundle:TeacherApply:view.php.twig",$data);
    }

}
